package com.cssy.xxkj.logic.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.xxkj.basic.entity.BasicMaterialsTypeDO;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.contants.BillTypeConstant;
import com.cssy.xxkj.logic.dao.LogicWarehouseBatchInDAO;
import com.cssy.xxkj.logic.dao.LogicWarehouseBatchInItemDAO;
import com.cssy.xxkj.logic.entity.LogicWarehouseBatchInDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseBatchInItemDO;
import com.cssy.xxkj.logic.service.ILogicHsUtilService;
import com.cssy.xxkj.logic.service.ILogicSalesService;
import com.cssy.xxkj.logic.service.ILogicWarehouseBatchInService;
import com.cssy.xxkj.logic.util.OftenMapUtil;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtils;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.exception.SyCodeException;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Service("logicWarehouseBatchInService")
public class LogicWarehouseBatchInServiceImpl implements
		ILogicWarehouseBatchInService {
	
	@Resource(name = "logicWarehouseBatchInDAO")
	private LogicWarehouseBatchInDAO logicWarehouseBatchInDAO;
	
	@Resource(name = "logicWarehouseBatchInItemDAO")
	private LogicWarehouseBatchInItemDAO logicWarehouseBatchInItemDAO;

	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Resource(name = "logicHsUtilService")
	private ILogicHsUtilService logicHsUtilService;
	
	@Resource(name = "logicSalesService")
	private ILogicSalesService logicSalesService;
	
	@Resource(name = "oftenMapUtil")
	private OftenMapUtil oftenMapUtil;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult sr= new SearchResult();
		sr.setResults(this.logicWarehouseBatchInDAO.find(scs));
		sr.setRecordCount(this.logicWarehouseBatchInDAO.count(scs));
		return sr;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer update(LogicWarehouseBatchInDO t, User user)
			throws SyBaseException {
		t.setBizModifyDate(new Date());
		t.setBizModifyName(user.getName());
		t.setBizVersionMark(t.getBizVersion());
		t.setBizVersion(t.getBizVersion()+1);
		return this.logicWarehouseBatchInDAO.updateLogicWarehouseBatchInDO(t);
	}

	@Override
	public LogicWarehouseBatchInDO save(LogicWarehouseBatchInDO t, User user)
			throws SyBaseException {
		Date date=new Date();
		t.setBizCreatorName(user.getName());
		t.setBizCreatorDate(date);
		t.setBizVersion(1);
		logicWarehouseBatchInDAO.insertLogicWarehouseBatchInDO(t);
		return t;
	}

	@Override
	public LogicWarehouseBatchInDO findListById(String id)
			throws SyBaseException {
		return logicWarehouseBatchInDAO.findLogicWarehouseBatchInDOByPrimaryKey(id);
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		return null;
	}

	@Override
	public void batchInSave(LogicWarehouseBatchInDO curAP,
			HttpServletRequest request) throws SyBaseException, SyCodeException {
		try {
			if(curAP==null){
				throw new SyBaseException("数据错误");
			}
			String item = request.getParameter("addressInfos");
			List<LogicWarehouseBatchInItemDO> itemList=JSONArray.parseArray(item, LogicWarehouseBatchInItemDO.class);
			if(StringUtils.isEmpty(curAP.getId())){//新增
				curAP.setZd001Code(syCodeService.getCode(BillTypeConstant.BATCHIN,BillTypeConstant.BATCHIN,SyCode.DATE_YYMM,5));
				String id = IDCodeTypeUtil.getId();
				curAP.setId(id);
				curAP.setZd004InDate(DateUtils.stringToDate(request.getParameter("zd004InDate")));
				this.save(curAP, SessionUtil.getUser(request));
				Map<String , BasicMaterialsTypeDO> typeMap = this.oftenMapUtil.getMaterialTypeKeyMaterialCode(new SearchConditions());
				for(LogicWarehouseBatchInItemDO bii:itemList){
					bii.setZd001BatchId(id);
					bii.setZd001BatchCode(curAP.getZd001Code());
					bii.setId(IDCodeTypeUtil.getId());
					bii.setZd011Series(typeMap.get(bii.getZd002MaterCode())==null?null:typeMap.get(bii.getZd002MaterCode()).getZd002Name());
					this.logicWarehouseBatchInItemDAO.insertLogicWarehouseBatchInItemDO(bii);
				}
				
			}else{//修改
				LogicWarehouseBatchInDO batchIn=this.logicWarehouseBatchInDAO.findLogicWarehouseBatchInDOByPrimaryKey(curAP.getId());
				if(batchIn==null){
					throw new SyBaseException("数据不存在，请确认本数据是否被删除");
				}
				if(BillStatusUtil.BATCH_IN_TWO.equals(curAP.getZd010Stauts())){//完成
					batchIn.setZd010Stauts(curAP.getZd010Stauts());
					int rt =this.update(batchIn, SessionUtil.getUser(request));
					if(0==rt){
						throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
					}
					LogicWarehouseBatchInItemDO batchInItem= new LogicWarehouseBatchInItemDO();
					batchInItem.setZd001BatchId(curAP.getId());
					List<LogicWarehouseBatchInItemDO> it= this.logicWarehouseBatchInItemDAO.findListByExample(batchInItem);
					for(LogicWarehouseBatchInItemDO bi:it){
						logicHsUtilService.processHsBatchInItem(batchIn, bi, SessionUtil.getUser(request));
					}
				}else if (BillStatusUtil.BATCH_IN_THREE.equals(curAP.getZd010Stauts())){//退回
					batchIn.setZd010Stauts(BillStatusUtil.BATCH_IN_THREE);
					int rt=this.update(batchIn, SessionUtil.getUser(request));
					if(0==rt){
						throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
					}
				}else{//保存 提交
					String zd004InDate =request.getParameter("zd004InDate");
					curAP.setZd004InDate(DateUtils.stringToDate(zd004InDate));
					int rt=this.update(curAP, SessionUtil.getUser(request));
					if(0==rt){
						throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
					}
					Map<String, String> parmMap=new HashMap<String, String>();
					parmMap.put("zd001BatchId", curAP.getId());
					this.logicWarehouseBatchInItemDAO.deleteLogicWarehouseBatchInItemByMap(parmMap);
					Map<String , BasicMaterialsTypeDO> typeMap = this.oftenMapUtil.getMaterialTypeKeyMaterialCode(new SearchConditions());
					for(LogicWarehouseBatchInItemDO bii:itemList){
						bii.setZd001BatchId(curAP.getId());
						bii.setZd001BatchCode(curAP.getZd001Code());
						bii.setId(IDCodeTypeUtil.getId());
						bii.setZd011Series(typeMap.get(bii.getZd002MaterCode())==null?null:typeMap.get(bii.getZd002MaterCode()).getZd002Name());
						this.logicWarehouseBatchInItemDAO.insertLogicWarehouseBatchInItemDO(bii);
					}
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new SyBaseException("数据错误");
		}
	}

}
